package com.kaili.basic.controller.web.v1;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.Wellendto;
import com.kaili.basic.pojo.vo.WellenVo;
import com.kaili.basic.service.PickManageService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@ApiVersion(1)
@Api(tags = "二次分拣管理", description = "二次分拣管理相关API")
@RequestMapping("/{api_version}")
public class TwoSortingController {


    Logger log = LoggerFactory.getLogger(TwoSortingController.class);

    @Autowired
    private PickManageService pickManageService;

    @GetMapping("/twoSorting")
    @ApiOperation(value = "二次分拣查询波次单",notes = "二次分拣查询波次单")
    public Page<WellenVo> queryWellenByPages(@ModelAttribute Wellendto wellendto){
        WellenVo wellenVo = new WellenVo();
        wellenVo.setWellenNo(wellendto.getWellenNo());
        wellenVo.setWellenStatus(wellendto.getWellenStatus());
        wellenVo.setPickStartTime(wellendto.getStartTime());
        wellenVo.setPickEndTime(wellendto.getEndTime());
        wellenVo.setPage(wellendto.getPage());
        wellenVo.setPageSize(wellendto.getPageSize());

        return pickManageService.queryTwoWellenByPages(new Page<WellenVo>(wellenVo.getPage(),wellenVo.getPageSize()),wellenVo);
    }

    @PostMapping("/twoSorting")
    @ApiOperation(value = "二次分拣修改波次单",notes = "二次分拣修改波次单")
    public CommonStatus updateWellen(@ModelAttribute Wellendto wellendto){

        pickManageService.updateWellen(wellendto);

        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/twoSortingExport")
    @ApiOperation(value = "二次分拣波次单导出",notes = "二次分拣波次单导出")
    public void export(@ModelAttribute Wellendto wellendto, HttpServletResponse response){
        WellenVo wellenVo = new WellenVo();
        wellenVo.setWellenNo(wellendto.getWellenNo());
        wellenVo.setWellenStatus(wellendto.getWellenStatus());
        wellenVo.setPickStartTime(wellendto.getStartTime());
        wellenVo.setPickEndTime(wellendto.getEndTime());
        wellenVo.setPage(wellendto.getPage());
        wellenVo.setPageSize(wellendto.getPageSize());
        try{
            ExcelUtil.exportExcel(pickManageService.queryTwoWellenByExcport(wellenVo),"二次分拣波次单管理","二次分拣波次单管理",WellenVo.class,"二次分拣波次单管理"+UUIDUtil.getUUId()+".xls",response);
        }catch (RestException e){
            log.error("二次分拣波次单导出异常"+e.getMessage());
        }
                ;
    }
}
